package com.aiyige.page.home;


import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aiyige.R;
import com.aiyige.arouter.Router;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.arouter.eventbus.EventHomePageRouter;
import com.aiyige.base.BaseFragment;
import com.aiyige.base.eventbus.EventLogin;
import com.aiyige.base.eventbus.EventLogout;
import com.aiyige.base.eventbus.EventTapBottomItemRefresh;
import com.aiyige.model.tag.Tag;
import com.aiyige.page.home.adapter.HomeViewPagerAdapter;
import com.aiyige.page.interest.event.EventSelectChannel;
import com.aiyige.page.interest.model.Interest;
import com.aiyige.page.interest.util.InterestUtil;
import com.aiyige.utils.AccountUtil;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.SlidingTabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class HomePage extends BaseFragment {
    public static final int REQUEST_CODE_SELECT_CHANNEL = 1122;

    @BindView(R.id.tabLayout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.mainVp)
    ViewPager mainVp;
    Unbinder unbinder;
    HomeViewPagerAdapter homeViewPagerAdapter;
    List<Interest> interestList = new LinkedList<>();

    public HomePage() {

    }

    public static HomePage newInstance() {
        HomePage fragment = new HomePage();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.page_home, container, false);
        unbinder = ButterKnife.bind(this, view);
        homeViewPagerAdapter = new HomeViewPagerAdapter(getChildFragmentManager());
        mainVp.setAdapter(homeViewPagerAdapter);
        mainVp.setOffscreenPageLimit(10);
        tabLayout.setViewPager(mainVp);
        preparePage();
        Router.registerRouterEventBus(this);
        return view;
    }

    public void preparePage() {
        List<Tag> avocation = AccountUtil.getCurrentUser().getAvocation();
        List<Interest> interestList = InterestUtil.convertTagListToInterestList(avocation);
        homeViewPagerAdapter.setNewData(interestList);
        tabLayout.setViewPager(mainVp);
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        Router.unregisterRouterEventBus(this);
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected void initData() {
        if (!EventBus.getDefault().isRegistered(this)) {
            try {
                EventBus.getDefault().register(this);
            } catch (Exception e) {

            }
        }
    }

    @OnClick({R.id.scanBtn, R.id.searchBar, R.id.selectInterestBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.scanBtn:
                ARouter.getInstance()
                        .build(ARouterConfig.ScanQRCodePage)
                        .navigation(getActivity());
                break;
            case R.id.searchBar:
                ARouter.getInstance()
                        .build(ARouterConfig.GlobalSearchPage)
                        .navigation();
                break;
            case R.id.selectInterestBtn:
                ARouter.getInstance()
                        .build(ARouterConfig.SelectChannelPage)
                        .withTransition(R.anim.activity_slide_in_bottom, R.anim.activity_still)
                        .navigation(getActivity());
                break;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventSelectChannel event) {
        if (event.isShouldRefreshChannel()) {
            mainVp.setCurrentItem(0, false);
            preparePage();
        }
        if (event.isShouldJump()) {
            mainVp.setCurrentItem(event.getJumpPosition() + 3, false);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventLogin eventLogin) {
        if (AccountUtil.isLogin()) {
            onEvent(EventSelectChannel.newBuilder()
                    .shouldRefreshChannel(true)
                    .shouldJump(false)
                    .build());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventLogout eventLogout) {
        onEvent(EventSelectChannel.newBuilder()
                .shouldRefreshChannel(true)
                .shouldJump(false)
                .build());
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventTapBottomItemRefresh event) {
        if (isVisible()
                && event.getItemType() == EventTapBottomItemRefresh.ITEM_TYPE_HOME
                && event.getActionType() == EventTapBottomItemRefresh.ACTION_TYPE_PRE_REFRESH) {
            EventBus.getDefault().post(EventTapBottomItemRefresh.newBuilder()
                    .itemType(EventTapBottomItemRefresh.ITEM_TYPE_HOME)
                    .actionType(EventTapBottomItemRefresh.ACTION_TYPE_START_REFRESH)
                    .interest(homeViewPagerAdapter.getPageInterface(mainVp.getCurrentItem()).getInterest())
                    .build());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(EventHomePageRouter event) {
        Router.eventBus.removeStickyEvent(event);
        if (event.getJumpIndex() < 3) {
            mainVp.setCurrentItem(event.getJumpIndex(), false);
        } else if (AccountUtil.isLogin()) {
            mainVp.setCurrentItem(event.getJumpIndex(), false);
        }
    }
}
